import {decode} from "../utils/StrUtils.ts";
import {Timestamp, TS} from "./Timestamp.ts";
import {parsePriMessageStatus, PriMessageStatus} from "./PriMessageStatus.ts";
import type {TinyUserInfo} from "./User.ts";
import {buildUserMap, type UserMap} from "./TopicDetail.ts";
import {type BbsCodeNode, parseContent} from "./BbsCodeNode.ts";

export interface PrivateMessage {
    id: number
    bit: number
    title: string
    timeCreated: Timestamp | undefined
    timeLastModified: Timestamp | undefined
    postCount: number
    fromUserId: number
    fromUsername: string | undefined
    lastReplyUid: number
    lastReplyUsername: string | undefined
    status: PriMessageStatus[]
    users: TinyUserInfo[]
    unread: boolean
}

export interface PrivateMessageBody {
    page: number
    size: number
    hasNext: boolean
    data: PrivateMessage[]
    total: number
}


export const buildPrivateMessageBody = (data: any): PrivateMessageBody => {
    const page = Number(data.currentPage)
    const size = Number(data.rowsPerPage)
    const list = Object.keys(data).filter((k: string) => !isNaN(Number(k))).map(k => buildPrivateMessage(data[k]));
    const hasNext = !!data.nextPage || list.length === size
    const total = hasNext ? (page + 1) * size : ((page - 1) * size + list.length)
    return {page, size, hasNext, data: list, total}
}

export const buildPrivateMessage = (data: any): PrivateMessage => {
    const bit = Number(data.bit)
    const users: TinyUserInfo[] = []

    const allUsers = data.all_user?.split('\t') || []
    for (let i = 0; i < allUsers.length; i += 2) {
        const userId = Number(allUsers[i])
        const username = allUsers[i + 1]
        users.push({userId, username})
    }
    return {
        id: Number(data.mid),
        bit, users,
        title: decode(data.subject) || "",
        timeCreated: TS(data.time),
        timeLastModified: TS(data.last_modify),
        postCount: Number(data.posts),
        fromUserId: Number(data.from),
        fromUsername: data.from_username,
        lastReplyUid: Number(data.last_from),
        lastReplyUsername: data.last_from_username,
        status: parsePriMessageStatus(bit),
        unread: parsePriMessageStatus(bit).includes(PriMessageStatus.UNREAD),
    }
}


// 私信回复内容
export interface PriMsg {
    data: any,
    title: string | undefined
    content: string
    nodes: BbsCodeNode[] | undefined
    userId: number
    id: number
    time: Timestamp | undefined
}

export interface PriMsgBody {
    page: number
    hasNext: boolean
    data: PriMsg[]
    total: number
    status: PriMessageStatus[]
    userMap: UserMap
}

export const buildPriMsg = (data: any): PriMsg => {
    return {
        data: data.data,
        title: data.subject,
        content: data.content,
        nodes: parseContent(data.content),
        userId: data.from,
        id: data.id,
        time: TS(data.time)
    }
}

export const buildPriMsgBody = (data: any): PriMsgBody => {
    // console.log(data)
    const size = 20
    const userMap = buildUserMap(data.userInfo)
    const page = Number(data.currentPage);
    const total = !!data.nextPage ? (page + 1) * size : ((page - 1) * size + data.length)

    return {
        page, total,
        hasNext: !!data.nextPage,
        data: data.allmsgs.map(buildPriMsg),
        status: parsePriMessageStatus(Number(data.subjectBit)),
        userMap
    }
}